#include <iostream>
#include <fstream>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
using std::cout;
using std::endl;
using std::ofstream;
using namespace log4cpp;

void test0(){   
    //1.设置布局
    PatternLayout * ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn3 = new PatternLayout();
    ptn3->setConversionPattern("%d %c [%p] %m%n");

    //2.创建目的地对象
    auto pos = new OstreamAppender("console",&cout);
    pos->setLayout(ptn1);

    auto pfile = new FileAppender("file","wd.log");
    pfile->setLayout(ptn2);

    auto roll = new RollingFileAppender("rollingFile",
                                        "rollingwd.log",
                                        5 * 1024,
                                        9);
    roll->setLayout(ptn3);


    //3.创建记录器
    Category & sales = Category::getRoot().getInstance("salesDepart");
    sales.setPriority(Priority::DEBUG);
    sales.addAppender(pos);
    sales.addAppender(pfile);
    sales.addAppender(roll);

    //4.记录日志
    int count = 100;
    while(count > 0){
        sales.emerg("this is an emerg msg");
        sales.fatal("this is a fatal msg");
        sales.alert("this is an alert msg");
        sales.crit("this is a crit msg");
        sales.error("this is an error msg");
        sales.warn("this is a warn msg");
        sales.notice("this is a notice msg");
        sales.info("this is an info msg");
        sales.debug("this is a debug msg");
        --count;
    }

    //5.日志系统退出时，回收资源
    Category::shutdown();
}

int main(void){
    test0();
    return 0;
}
